package com.platform_server.controller;

import com.platform_server.entity.db.Admin;
import com.platform_server.entity.pojo.AdminLoginParam;
import com.platform_server.entity.response.Header;
import com.platform_server.entity.response.QueryResponse;
import com.platform_server.entity.response.ResponseBase;
import com.platform_server.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @Author：Kevin.W.Liu
 * @Date: 2022/3/12
 * @Version 1.0
 */
@RestController
public class LoginController {
    @Autowired
    private IAdminService adminService;

    /**
     * 登录之后返回token
     * @param adminLoginParam
     * @param request
     * @return
     */
    @PostMapping("/login")
    public ResponseBase login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),request);
    }

    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (null==principal){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username);
        admin.setPassword(null);
        return admin;
    }

    @PostMapping("/logout")
    public ResponseBase logout(){
        QueryResponse queryResponse = new QueryResponse();
        queryResponse.setHeader(new Header(200,"注销成功!"));
        return queryResponse;
    }
}
